 rem have used variables c,d,s,t,i,q,r,g,e,f,h,k
   set legacy 0.99
   set tv ntsc
   set romsize 4k
   include div_mul.asm
 rem sets up player sprite to be neutral up
   player0:
 %00011000
 %00111100
 %11111111
 %01111110
 %00111100
 %00111100
 %00011000
 %00000000
end
 rem these are the lengths of each segment of playfield to be randomly chosen and scrolled through
   data playlengths
   2, 61, 35, 60
end
 rem this is the converted playfield data for the maze section of playfield
   data mazeplay
 224,0,0,128,
 176,0,0,128,
 188,0,0,128,
 255,0,0,128,
 191,3,0,160,
 191,7,0,176,
 255,3,0,252,
 191,0,0,191,
 188,0,1,191,
 248,0,7,255,
 176,0,31,191,
 160,0,127,191,
 224,0,63,255,
 176,0,15,191,
 184,0,3,191,
 255,0,0,255,
 191,3,0,188,
 191,7,0,128,
 255,7,0,128,
 191,3,0,176,
 191,0,0,188,
 252,0,0,255,
 184,0,3,191,
 176,0,15,191,
 240,0,31,255,
 176,0,63,191,
 176,0,31,191,
 248,0,15,255,
 188,0,7,191,
 190,0,1,191,
 255,0,0,254,
 159,3,0,156,
 159,15,0,176,
 191,63,0,160,
 159,255,0,128,
 159,255,192,128,
 143,255,240,128,
 175,255,248,160,
 167,131,252,160,
 135,1,252,160,
 131,0,248,160,
 129,128,240,128,
 128,192,224,128,
 128,240,192,128,
 128,252,0,160,
 128,63,0,168,
 131,3,0,178,
 175,0,1,185,
 188,0,7,188,
 176,0,14,184,
 160,0,14,160,
 160,0,7,161,
 128,0,1,191,
 128,0,0,190,
 128,0,128,184,
 160,0,64,160,
 188,0,48,160,
 191,1,56,128,
 191,3,60,160,
 191,3,24,160,
 255,0,0,224,
 184,0,0,160
end
 rem this is the converted playfield data for the asteroids section of playfield
  data rocksplay
  227,0,0,224,
  163,0,6,160,
  160,0,6,160,
  224,48,0,224,
  160,48,0,160,
  160,3,0,166,
  224,3,96,230,
  160,0,96,160,
  160,0,3,160,
  227,0,3,224,
  163,96,0,160,
  160,96,0,160,
  224,0,12,224,
  160,0,12,160,
  160,0,0,160,
  224,0,96,224,
  160,0,96,160,
  160,0,0,160,
  224,12,0,224,
  160,12,0,160,
  160,0,12,160,
  224,0,12,224,
  160,0,0,160,
  160,0,0,160,
  224,0,192,224,
  160,0,192,160, 
  160,0,0,160,
  224,24,0,224,
  160,24,0,160,
  160,0,0,160,
  224,0,0,224,
  160,0,3,160,
  160,0,3,160,
  225,1,0,224,
  161,1,0,160,
  160,0,0,160
end
 rem this is the converted set of data for the target section of playfield
   data targetplay
 224,0,0,224,
 160,0,8,160,
 160,0,20,128,
 224,0,34,128,
 160,0,85,128,
 165,10,0,128,
 226,4,0,224,
 129,2,0,160,
 128,1,0,160,
 128,0,0,224,
 128,0,0,160,
 128,0,0,160,
 224,0,0,224,
 160,0,0,128,
 160,0,0,128,
 224,32,0,128,
 128,96,0,128,
 128,160,0,128,
 128,32,128,224,
 160,32,64,160,
 160,64,32,160,
 130,128,16,160,
 131,0,136,128,
 162,1,68,128,
 130,2,34,128,
 130,4,17,128,
 130,8,9,128,
 162,16,5,160,
 162,32,3,160,
 129,64,1,160,
 128,129,0,160,
 128,130,0,128,
 128,130,0,128,
 128,130,0,128,
 128,130,0,128,
 128,68,0,160,
 128,40,0,164,
 160,16,0,166,
 160,0,0,165,
 160,0,1,164,
 224,0,2,228,
 160,0,4,164,
 160,0,8,164,
 224,0,31,231,
 128,42,0,128,
 128,20,2,133,
 128,8,1,130,
 128,0,0,161,
 128,0,0,160,
 224,64,160,224,
 160,128,64,128,
 160,0,128,128,
 225,10,0,128,
 160,5,0,128,
 160,2,0,128,
 224,0,0,224,
 128,0,0,160,
 128,0,0,160,
 128,0,0,224,
 160,0,0,160,
 160,0,0,160
end
 rem this is a small set of converted playfield data where it is an enlongated straightaway section, so we just reused the same 3 lines to create that section
   data loopplay
   224,0,0,224,
   160,0,0,160,
   160,0,0,160
end   
 rem this is the set of data for the music that we dont have implemented
   sdata mymusic=a
   1,2,3,4,5,6,7,255
end
 rem this is the title splash screen for SPACE RACE
start
 playfield:
 X..............................X
 X..XXXX.XXX...X...XXXX.XXXX....X
 X..X....X.X..X.X.X.....XXXX....X
 X....XX.XXX..XXX.X.....X.......X
 X..XXXX.X....X.X..XXXX.XXXX....X
 X..............................X
 X..XXXX..XX...XXXX.XXXX........X
 X..X..X.X..X.X.....XXXX........X
 X..XXX..XXXX.X.....X...........X
 X..X..X.X..X..XXXX.XXXX........X
 X..............................X
end
 rem this sets up the title's settings and waits for the user to be ready to start the game
title
 COLUBK = $06
 COLUPF = $76
 drawscreen
 if joy0fire then goto skiptitle
 goto title
 rem this sets up the game for the first time, drawing the first playfield and ball
skiptitle
 pfclear
 ballx = 153
 bally = 86
 playfield:
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 XXX..........................XXX
end
 rem this initializes all variables, sets the player, score, ball, and missile on the screen properly and sets up screen scrolling
init
   c = 0
   d = 1 : rem 0 is nonactive missile, 1 is fired up, 2 is fired left, 3 is fired right
   q = 0 : rem used when deleting playfield blocks, the translated x value
   r = 0 : rem used when deleting playfield blocks, the translated y value
   score = 888888
   scback = $A8
   player0x = 100
   player0y = 90
   ballheight = 2 : rem * Ball 2 pixels high.
   CTRLPF = $11 : rem * Ball 2 pixels wide.
   COLUPF = $1E : rem * Ball/playfield color.
   dim scback = $A4
   COLUP0 = $74
   COLUPF = $A8
   missile0height = 4
   scorecolor = 10
   
   dim scrollline = s
   scrollline = 0
   dim loopcount = e
   loopcount = 3
   dim loopcheck = f
   loopcheck = 0
   dim currentstate = g
   currentstate = 1
   dim nxtstate = h
   nxtstate = 0
   dim ballhitcount = k
   ballhitcount = 0
   dim laststate=l
   laststate = 1
   
   goto Main

 rem this is the main loop which handles most gameplay
Main

   COLUBK = $A2 : rem sets background color
   
   if switchreset then init
   
   pfscroll down : rem scrolls the screen
   c = c + 1 : rem counter for how many times the screen has been scrolled
   score = score - 1 :  rem constantly decreasing score
   t = sread(mymusic) : rem more code for the music that doesnt exist
   
   if missile0y>240 then goto skip 
   if missile0x < 37 || missile0x > 175 then goto skip
   
 rem this set of conditionals controls the missile movement depending on the user's desired direction
   if d = 0 then missile0x = player0x: missile0y=player0y
   if d = 1 then missile0y = missile0y - 1 : goto draw
   if d = 2 then missile0x = missile0x - 1 : goto draw
   if d = 3 then missile0x = missile0x + 1 : goto draw
   
 rem this is the missile collision detector
skip
   missile0y=player0y-2:missile0x=player0x+4 : d = 0
 rem this keeps the player movement updated, loops the playfields, and checks if the player dies, deletes a block, or scores points
draw   
   gosub playermovement
   drawscreen
   if player0x > 140 then player0x = 139
   if player0x < 30 then player0x = 31
   
   on g gosub loopplayfield maze rocks target
   
   if collision(player0, playfield) then reboot
   if collision(playfield, missile0) then  gosub deleteplayfield: missile0y=player0y-2:missile0x=player0x+4 : d = 0
   if collision(missile0, ball) then gosub drawball : ballhitcount = ballhitcount + 1 : score = score + 035000
   if ballhitcount = 4 then gosub win : rem this is the win condition, threshold at 3 successful ball shots
   
   goto Main
 rem this is a playfield which is rather plain and straight
loopplayfield
   if c = 8 then gosub playloop
   if loopcount = loopcheck then gosub looprandomize
 return
 rem this is a playfield segment which requires much player movement
maze
   if c = 8 then gosub playmaze
   if scrollline = playlengths[1] then laststate = 1 : gosub randomize
 return
 rem this is a playfield segment which requires player decision making
rocks
   if c = 8 then gosub playrocks
   if scrollline = playlengths[2] then laststate = 2 :  gosub randomize
 return
 rem this is a playfield segment which allows the player to shoot the target more easily
target
   if c = 8 then gosub playtarget
   if scrollline = playlengths[3] then laststate = 3 : gosub randomize
 return
 
looprandomize
   nxtstate = (rand&3)
   if nxtstate = laststate then nxtstate = nxtstate + 1
   if nxtstate = 0 then nxtstate = 3
   if nxtstate > 3 then nxtstate = 1
   currentstate=nxtstate
   loopcheck=0
   scrollline = 0
 return 
 
randomize
   currentstate = 0
   scrollline = 0
 return  

 rem this determines how the missile should look when fired, depending on the direction as well as how the player should look always   
playermovement
   if joy0fire && m = 1 then missile0y=player0y:missile0x=player0x+(9/2) : missile0height = 4 : NUSIZ0=$10 : d = 1
   if joy0fire && m = 2 then missile0y=player0y-3:missile0x=player0x+(9/2) : missile0height = 1 : NUSIZ0=$20 : d = 2
   if joy0fire && m = 3 then missile0y=player0y-3:missile0x=player0x+(9/2) : missile0height = 1 : NUSIZ0=$20 : d = 3
   if joy0up then m = 1 : gosub changeup
   if joy0left then gosub changeleft : player0x = player0x - 1 :  m = 2 : REFP0 = 8
   if joy0right then player0x = player0x + 1 :  m = 3 : REFP0 = 0 : gosub changeright
 return
 rem sprite data for when the player is facing left
changeleft
   player0:
 %01111111
 %11111111
 %11111100
 %01111000
 %00111000
 %00110000
 %00010000
 %00000000
end 
 return
 rem sprite data for when the player is facing right
changeright
   player0:
 %11111110
 %11111111
 %00111111
 %00011110
 %00011100
 %00001100
 %00001000
 %00000000
end 
 return 
 rem sprite data for when the player is facing up
changeup 
   player0:
 %00011000
 %00111100
 %11111111
 %01111110
 %00111100
 %00111100
 %00011000
 %00000000
end   
 return  
 rem this is the algorithm for the small straight section that directly reads in each value in a playfield segment's data array
 rem intelligently so that it may assign each of the 4 variables of the hidden row the proper value to be scrolled and subsequently
 rem redrawn at the top of the screen, resulting in infinite and randomly chosen state-dependent vertical scrolling
playloop
   c = 0
   i=4*(playlengths[0]-scrollline)
   var44=loopplay[i]
   i=i+1 
   var45=loopplay[i]
   i=i+1
   var46=loopplay[i]
   i=i+1
   var47=loopplay[i]
   scrollline = scrollline + 1 
   j= playlengths[0]
   if scrollline > j then scrollline = 0 : loopcheck = loopcheck + 1
 return 
 rem this is the algorithm for the target section that directly reads in each value in a playfield segment's data array
 rem intelligently so that it may assign each of the 4 variables of the hidden row the proper value to be scrolled and subsequently
 rem redrawn at the top of the screen, resulting in infinite and randomly chosen state-dependent vertical scrolling
playtarget
   c = 0
   i=4*(playlengths[3]-scrollline)
   var44=targetplay[i]
   i=i+1 
   var45=targetplay[i]
   i=i+1
   var46=targetplay[i]
   i=i+1
   var47=targetplay[i]
   scrollline = scrollline + 1 
   if scrollline > playlengths[3] then scrollline = 0
 return
 rem this is the algorithm for the asteroids section that directly reads in each value in a playfield segment's data array
 rem intelligently so that it may assign each of the 4 variables of the hidden row the proper value to be scrolled and subsequently
 rem redrawn at the top of the screen, resulting in infinite and randomly chosen state-dependent vertical scrolling
playrocks
   c = 0
   i=4*(playlengths[2]-scrollline)
   var44=rocksplay[i]
   i=i+1 
   var45=rocksplay[i]
   i=i+1
   var46=rocksplay[i]
   i=i+1
   var47=rocksplay[i]
   scrollline = scrollline + 1 
   if scrollline > playlengths[2] then scrollline = 0
 return
 rem this is the algorithm for the maze that directly reads in each value in a playfield segment's data array
 rem intelligently so that it may assign each of the 4 variables of the hidden row the proper value to be scrolled and subsequently
 rem redrawn at the top of the screen, resulting in infinite and randomly chosen state-dependent vertical scrolling
playmaze
   c = 0
   i=4*(playlengths[1]-scrollline)
   var44=mazeplay[i]
   i=i+1 
   var45=mazeplay[i]
   i=i+1
   var46=mazeplay[i]
   i=i+1
   var47=mazeplay[i]
   scrollline = scrollline + 1 
   if scrollline > playlengths[1] then scrollline = 0
 return
 rem this is the win splash screen, seen only after shooting the ball 3 times
win 
 drawscreen
 playfield:
 X.X.X.XXX...XXX............X.X.X
 X.X.X..XX...XX.............X.X.X
 X.X.X..XX.X.XX.............X.X.X
 X.X.X...XXXXX..............X.X.X
 X.X.X.......XXXXXX.........X.X.X
 X.X.X.........XX...........X.X.X
 X.X.X.........XX...........X.X.X
 X.X.X.......XXXXXX.........X.X.X
 X.X.X............XXX..XX...X.X.X
 X.X.X............XXXX.XX...X.X.X
 X.X.X............XX.XXXX...X.X.X
 X.X.X............XX..XXX...X.X.X 
end
 if joy0fire then reboot
 goto win
 rem this deletes any playfield block that a player hit using a vertical missile half the time
deleteplayfield
   q = (missile0x-32) /4
   r = missile0y/8
   if d = 1 && pfread(q,r) then pfpixel q r off 
 return
 rem this toggle's the ball's position after being shot by the player
drawball
   if ballhitcount = 2 then  ballx = 153
   if ballhitcount = 1 || ballhitcount = 3 then ballx = 8
 return
 rem this is some assembly code that allows for our flashing score bar background
   asm
minikernel
   sta WSYNC
   lda scback
   sta COLUBK
   rts
end